<template>
    <div class="goods-container">

        <!--<router-link :to="'/home/goodsinfo/' + item.id" tag="div" class="goods-item"-->
                     <!--v-for="item in goodsList" :key="item.id">-->
            <!--<img :src="item.img_url" alt="">-->

            <!--<h4>{{ item.title }}</h4>-->

            <!--<div class="info">-->
                <!--<p class="price">-->
                    <!--<span class="now">￥{{ item.sell_price }}</span>-->
                    <!--<span class="old">￥{{ item.market_price }}</span>-->
                <!--</p>-->
                <!--<p class="sell">-->
                    <!--<span>热卖中</span>-->
                    <!--<span>剩{{ item.stock_quantity }}件</span>-->
                <!--</p>-->
            <!--</div>-->
        <!--</router-link>-->

        <!-- 用this.$router进行跳转 -->
        <div
                @click="goGoodsInfo(item.id)"
                class="goods-item"
                v-for="item in goodsList" :key="item.id">
            <img :src="item.img_url" alt="">

            <h4>{{ item.title }}</h4>

            <div class="info">
                <p class="price">
                    <span class="now">￥{{ item.sell_price }}</span>
                    <span class="old">￥{{ item.market_price }}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{ item.stock_quantity }}件</span>
                </p>
            </div>
        </div>

        <mt-button
                @click="getMoreGoods"
                class="getMore"
                type="danger"
                size="large">加载更多</mt-button>

    </div>
</template>

<script>
    import { Toast } from 'mint-ui'

    export default {
        name: "",
        data(){
            return {
                pageIndex: 1,
                goodsList: []
            }
        },
        created(){
            this.getGoodsList()
        },
        methods: {
            getGoodsList(){
                this.$http.get('static/data/goodsList.json').then(res => {
                    if(res.body.status === 0){
                        console.log(res);
                        var ret = res.body.message.find(item => item.pageIndex===this.pageIndex)
                        console.log(ret);
                        if(!ret) return Toast('商品获取失败')
                        this.goodsList = this.goodsList.concat(ret.goods)
                    }
                })
            },
            getMoreGoods(){
                this.pageIndex++
                this.getGoodsList()
            },
            goGoodsInfo(id){
                // this.$route  是路由【参数对象】
                // this.$router 是路由【导航对象】
                // this.$router.push('/home/goodsinfo/' + id)

                this.$router.push({ path: '/home/goodsinfo/' + id })

                // 此种方式name对应的是路由中添加的name属性值
                // this.$router.push({ name: '', params: { id: id } })
            }
        }
    }
</script>

<style lang="less" scoped>
.goods-container{
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 0px 6px;
    display: flex;
    .goods-item{
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        min-height: 285px;
        width: 49%;
        border: 1px solid #CCCCCC;
        box-shadow: 0 0 6px #ccc;
        padding: 2px;
        margin: 4px 0 0;
        img{
            width: 100%;
        }
        h4{
            font-size: 14px;
            padding: 0 3px;
        }
        .info{
            background-color: #eee;
            p{
                margin: 0;
                line-height: 28px;
                font-size: 13px;
                padding: 0 4px;
            }
            .price{
                .now{
                    font-size: 16px;
                    color: red;
                    font-weight: 600;
                }
                .old{
                    text-decoration: line-through;
                }
            }
            .sell{
                display: flex;
                justify-content: space-between;
            }
        }
    }
    .getMore{
        margin: 8px 0;
    }
}
</style>